/*******************************************************************************
 * Copyright 2011 Alexandre Zglav and Morphiastic
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.morphiastic.analysis.filters;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import com.morphiastic.analysis.AnalysisComponentDefinition;
import com.morphiastic.utils.EqualsUtil;
/**
 * A token filter of type stop that removes stop words from token streams.
 * 
 * The following are settings that can be set for a stop token filter type:
 * 
 * Setting Description 
 * stopwords A list of stop words to use. Defaults to
 * english stop words. 
 * stopwords_path A path (either relative to config
 * location, or absolute) to a stopwords file configuration.
 * 
 * enable_position_increments Set to true if token positions should record the
 * removed stop words, false otherwise. Defaults to true. 
 * 
 * ignore_case Set to true to lower case all words first. Defaults to false. 
 * 
 * stopwords allow for
 * custom language specific expansion of default stopwords. It follows the
 * _lang_ notation and supports: arabic, armenian, basque, brazilian, bulgarian,
 * catalan, czech, danish, dutch, english, finnish, french, galician, german,
 * greek, hindi, hungarian, indonesian, italian, norwegian, persian, portuguese,
 * romanian, russian, spanish, swedish, turkish.
 * 
 * @author Alex
 * 
 */
public class StopTokenFilter extends ElasticFilter{

	
	public static final String STOPWORDS_SEPARATOR = ",";
	public static final String STOPWORDS_PARAM_KEY = "stopwords";
	public static final String STOPWORDS_PATH_PARAM_KEY = "stopwords_path";
	public static final String ENABLE_POSITION_INCREMENTS_PARAM_KEY = "enable_position_increments";
	public static final String IGNORE_CASE_PARAM_KEY = "ignore_case";
	
	
	private Set<String> stopwords;
	private String stopWordsPath;
	private Boolean enablePositionIncrements;
	private Boolean ignoreCase;
	
	
	public StopTokenFilter(String defName){
		this(null,defName);
	}
	
	
	public StopTokenFilter(Map<String,String> params, String defName){
		
		super(ElasticFilterType.stop, defName);
		
		if(params!=null){
			String stopwStr = params.get(STOPWORDS_PARAM_KEY);
			if(stopwStr!=null && !stopwStr.isEmpty()){
				this.stopwords=new HashSet<String>();
				String[] stopwds = stopwStr.split(STOPWORDS_SEPARATOR);
				for (String stopw : stopwds) {
					this.stopwords.add(stopw.trim());
				}
			}
			this.stopWordsPath = params.get(STOPWORDS_PATH_PARAM_KEY); 
			this.enablePositionIncrements = Boolean.parseBoolean(params.get(ENABLE_POSITION_INCREMENTS_PARAM_KEY));
			this.ignoreCase = Boolean.parseBoolean(params.get(IGNORE_CASE_PARAM_KEY));
			
		}
	}
	
	
	@Override
	public boolean hasCustomParams() {
		 if(this.stopwords!= null || 
			(this.stopWordsPath!=null && !this.stopWordsPath.isEmpty()) ||
			this.enablePositionIncrements != null ||
			this.ignoreCase != null
		 )return true;
		 return false;
	}
	
	
	
	
	@Override
	public XContentBuilder addToBuilder(XContentBuilder mappingBuilder) throws IOException {
		mappingBuilder.startObject(getDefinitionName())
		.field("type", getComponentSubTypeName());
		
		//add custom params
		if(this.stopwords!=null){
			List<String> stopWrdsList = new ArrayList<String>(this.stopwords);
			mappingBuilder.field(STOPWORDS_PARAM_KEY, stopWrdsList);
		}
		if(this.stopWordsPath != null && !this.stopWordsPath.isEmpty()){
			mappingBuilder.field(STOPWORDS_PATH_PARAM_KEY, this.stopWordsPath);
		}
		if(this.enablePositionIncrements != null){
			mappingBuilder.field(ENABLE_POSITION_INCREMENTS_PARAM_KEY, this.enablePositionIncrements.booleanValue());			
		}
		if(this.ignoreCase != null){
			mappingBuilder.field(IGNORE_CASE_PARAM_KEY, this.ignoreCase);
		}
		return mappingBuilder;
	}
	

	
	
	@Override
	public void setParamsFromSettings(Settings settings) {
		//STOP WORDS SETTINGS
		Settings stopWordsSettings = settings.getByPrefix("stopwords.");
		if (stopWordsSettings.getAsMap().size()>0)this.stopwords = new HashSet<String>();
		for (Entry<String, String> stopWordSetting : stopWordsSettings.getAsMap().entrySet()) {
			this.stopwords.add(stopWordSetting.getValue());
		}
		
		String stopWordsPathSetting = settings.get(STOPWORDS_PATH_PARAM_KEY);
		if(stopWordsPathSetting!=null && !stopWordsPathSetting.isEmpty()){
			this.stopWordsPath = stopWordsPathSetting;
		}
		
		String enableSetting = settings.get(ENABLE_POSITION_INCREMENTS_PARAM_KEY);
		if(enableSetting!=null && !enableSetting.isEmpty()){
			this.enablePositionIncrements = Boolean.parseBoolean(enableSetting);
		}
		
		String ignoreCaseSetting = settings.get(IGNORE_CASE_PARAM_KEY);
		if(ignoreCaseSetting!=null && !ignoreCaseSetting.isEmpty()){
			this.ignoreCase = Boolean.parseBoolean(ignoreCaseSetting);
		}
	}
	

	
	
	

	@Override
	public boolean areEquivalentDefinitions(AnalysisComponentDefinition other){
		
		if(other==null)return false;
		StopTokenFilter o;
		try {
			o = (StopTokenFilter)other;
		} catch (ClassCastException e) {
			return false;
		}
		
		return 
		EqualsUtil.areEqual(this.stopwords, o.stopwords)
		&&
		EqualsUtil.areEqual(this.stopWordsPath, o.stopWordsPath)
		&&
		EqualsUtil.areEqual(this.enablePositionIncrements, o.enablePositionIncrements)
		&&
		EqualsUtil.areEqual(this.ignoreCase, o.ignoreCase);
	}
	
	
	
	
	
}
